package com.jsnkj.basic.activity

import android.content.Intent
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import com.jsnkj.basic.LibraryConfig
import com.jsnkj.basic.R
import com.jsnkj.basic.config.SpConfig
import com.jsnkj.basic.databinding.ActivityLoginBinding
import com.jsnkj.basic.mvvm.BaseMVVMActivity
import com.jsnkj.basic.util.AppUtil
import com.jsnkj.basic.util.ErrorResultUtil
import com.jsnkj.basic.util.LogUtils
import com.jsnkj.basic.util.SharePreferencesUtil
import com.jsnkj.basic.util.ToastUtils
import com.yang.http.HttpRequest
import com.yizooo.tallybook.TallyMainActivity

class LoginActivity : BaseMVVMActivity<ActivityLoginBinding, LoginViewModel>(),
    View.OnClickListener {
    override val layoutId: Int
        get() = R.layout.activity_login


    var ischeck = false


    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this
        setAgreementText()


        mVM.errorResult.observe(this) {
            dismissDialog()

            ErrorResultUtil.errorResultToast(this@LoginActivity, it)
            LogUtils.e("登录失败：$it")
        }

        mVM.postHttpResult.observe(this) {
            SharePreferencesUtil.getInstance().putUserBean(it)
            SharePreferencesUtil.getInstance().putBoolean(SpConfig.IS_LOGIN, true)

            it.access_token?.let { token ->
                HttpRequest.clearServiceMap()
                HttpRequest.addDefaultHeader("Authorization", "Bearer $token")
                HttpRequest.addDefaultHeader("APPCODE", LibraryConfig.VERSION_CODE.toString())
            }

            com.jsnkj.basic.burialpoint.BurialPointUtil.loginEvent("socical_wx")

            dismissDialog()
            ToastUtils.show("登录成功")
            startActivity(MainActivity::class.java)
            onBackPressed()
        }

        mVDB.imgLogo.setOnClickListener {
            if (LibraryConfig.DEBUG) {
                showDialog()
                mVM.postAuthLogin("ouD4f6_Jc9mpaxsXFOp47hjvV9oc")
            }
        }

        if (LibraryConfig.isEnableLoginType()) {
            mVDB.btnLogin.visibility = View.VISIBLE
            mVDB.tvOneKeyLogin.visibility = View.GONE
        } else {
            mVDB.btnLogin.visibility = View.GONE
            mVDB.tvOneKeyLogin.visibility = View.VISIBLE
        }
    }


    private fun setAgreementText() {
        val spanString = SpannableString("我已阅读并同意我们的《隐私政策》\n以及《用户协议》")
        spanString.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
               LogUtils.e("隐私政策")
                //隐私政策
                WebViewActivity.startWebActivity(
                    this@LoginActivity, "隐私政策", SpConfig.PRIVACY_POLICY_URL
                )
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = getColor(R.color.color_ea582a) //设置颜色
                ds.setUnderlineText(false);    //去除超链接的下划线
            }
        }, 11, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        spanString.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                LogUtils.e("用户协议")
                //用户协议
                WebViewActivity.startWebActivity(
                    this@LoginActivity, "用户协议", SpConfig.AGREEMENT_URL
                )
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = getColor(R.color.color_ea582a) //设置颜色
                ds.isUnderlineText = false;    //去除超链接的下划线
            }
        }, 19, spanString.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        mVDB.txtAgreement.text = spanString
        mVDB.txtAgreement.movementMethod = LinkMovementMethod.getInstance();//必须设置否则无效


        mVDB.tvVersion.text = AppUtil.getVersionName(this@LoginActivity)
    }


    override fun onClick(view: View) {

        when (view.id) {
            R.id.img_agreement -> {
                ischeck = !ischeck
                if (ischeck) {
                    mVDB.imgAgreement.setImageResource(R.drawable.icon_check_true)
                } else {
                    mVDB.imgAgreement.setImageResource(R.drawable.icon_check_false)
                }
            }

            R.id.btn_login, R.id.tv_one_key_login -> {

                if (!ischeck) {
                    ToastUtils.show("请勾选上方的隐私政策和用户协议")
                    return
                }
                intent = Intent(this@LoginActivity,
                    TallyMainActivity::class.java)
                startActivity(intent);

//                if (LibraryConfig.isEnableLoginType()) {
//                    com.jsnkj.basic.wxapi.WechatHelper.getInstance(
//                        this@LoginActivity.application, LibraryConfig.WX_APP_ID
//                    ).wechatLogin(this@LoginActivity, object :
//                        com.jsnkj.basic.wxapi.OnThirdLoginListener {
//                        override fun onSuccess(wxCode: String?) {
//                            LogUtils.e("wxCode == " + wxCode)
//                            wxCode?.let {
//                                showDialog()
//                                mVM.postOauthLogin(wxCode, "wechat")
//                            }
//                        }
//
//                        override fun onFailed() {
//                            ToastUtils.show( LibraryConfig.WX_APP_ID)
//                            dismissDialog()
//                            ToastUtils.show("微信登录授权失败")
//                        }
//
//                        override fun onCancel() {
//                            dismissDialog()
//                            ToastUtils.show("您已取消登录")
//                        }
//
//                    })
//                } else {
//                    showDialog()
//                    mVM.postOauthLogin(AppUtil.getAPPUUID(this@LoginActivity), "uuid")
//                }

            }

            else -> {
            }
        }
    }


}











